package com.hsj.busmanager.util;

import com.hsj.busmanager.api.UserBean;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 类名 ：
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2018/12/25 12:11
 */
public class CommonUtil {
    public static String getDateByFormat(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);

         return  sdf.format(date);
    }


    public static List<String> getImgSrcList(String content){
        List<String> list = new ArrayList<String>();
//目前img标签标示有3种表达式
//<img alt="" src="1.jpg"/> <img alt="" src="1.jpg"></img> <img alt="" src="1.jpg">
//开始匹配content中的<img />标签
        Pattern p_img = Pattern.compile("<(img|IMG)(.*?)(/>|></img>|>)");
        Matcher m_img = p_img.matcher(content);
        boolean result_img = m_img.find();
        if (result_img) {
            while (result_img) {
//获取到匹配的<img />标签中的内容
                String str_img = m_img.group(2);
//开始匹配<img />标签中的src
                Pattern p_src = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
                Matcher m_src = p_src.matcher(str_img);
                if (m_src.find()) {
                    String str_src = m_src.group(3);
                    list.add(str_src);
                }
//匹配content中是否存在下一个<img />标签，有则继续以上步骤匹配<img />标签中的src
                result_img = m_img.find();
            }
        }
        return list;
    }


    /**
     * 防止特殊字符（括号、引号等）被转义
     * @param imgName
     * @return
     */
    public static String decode(String imgName) {

        if(imgName == null){
            return null;
        }

        String result;
        try {
            result = URLDecoder.decode(imgName,"utf-8");
        }catch (Exception e){
            throw new RuntimeException("转义失败");
        }

        return result;
    }


    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }


    /**
     * 将list平均分成list.size/pageSize份
     * @param list
     * @param pageSize
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitList(List<T>list, int pageSize) {
        int listSize = list.size();
        int page = (listSize + (pageSize - 1)) / pageSize;
        List<List<T>>listArray = new ArrayList<List<T>>();
        for (int i = 0; i<page; i++) {
            List<T> subList = new ArrayList<T>();
            for (int j = 0; j<listSize; j++) {
                int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize;
                if (pageIndex == (i + 1)) {
                    subList.add(list.get(j));
                }
                if ((j + 1) == ((j + 1) * pageSize)) {
                    break;
                }
            }
            listArray.add(subList);
        }
        return listArray;
    }


    public static double getDistance(double lat_a, double lng_a, double lat_b, double lng_b){
        double pk = 180 / 3.14169;
        double a1 = lat_a / pk;
        double a2 = lng_a / pk;
        double b1 = lat_b / pk;
        double b2 = lng_b / pk;
        double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
        double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
        double t3 = Math.sin(a1) * Math.sin(b1);
        double tt = Math.acos(t1 + t2 + t3);
        return 6371000 * tt;
    }

    /**
     * 随机生成福州地区的经度
     * @return
     */
    public static double getLon(){
        //随机生成坐标
        //lon的取值范围是121.4600-121.4800 119.2800-119.3000
        //lat的取值范围是31.2200-31.2800 26.0800-26.26.1500
        Random randomLon = new Random();
        Random randomLat = new Random();
        double lon = ((double)(1192800+randomLon.nextInt(500)))/10000;
        double lat = ((double)(260800+randomLon.nextInt(700)))/10000;
        return lon;
    }

    /**
     * 随机生成福州地区的纬度
     * @return
     */
    public static double getLat(){
        //随机生成坐标
        //lon的取值范围是121.4600-121.4800
        //lat的取值范围是31.2200-31.2800
        Random randomLon = new Random();
        Random randomLat = new Random();
        double lon = ((double)(1192800+randomLon.nextInt(500)))/10000;
        double lat = ((double)(260800+randomLon.nextInt(700)))/10000;
        return lat;
    }

    public static void main(String[] args) {
        System.out.println(CommonUtil.getLat());

        System.out.println(CommonUtil.getLon());
    }

    public static boolean listNotNull(List<?> list) {

        if(list != null && list.size()>0){
            return true;
        }else{
            return false;
        }

    }

    public static Date parseDate(String showDriverTime, String pattern) throws Exception{

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);

        return sdf.parse(showDriverTime);
    }
}
